package com.shiguiwu.springmybatis.javabase.annontation;

import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotatedElementUtils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @description: spring
 * @author: stone
 * @date: Created by 2021/9/13 10:48
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.annontation
 */
public class SpringAnnontationTests {

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @interface Ann1 {
        String value() default "ann1";

    }

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Ann1
    @interface Ann2 {
        String value() default "ann2";

        /**
         * 这个相当于给某个注解指定别名，即将B1注解中 a14Value 参数作为 A14 中 value 参数的别名，当给
         * B1的a14Value 设置值的时候，就相当于给 A14的value设置值 ，有个前提是@AliasFor注解的
         * annotation 参数指定的注解需要加载当前注解上面
         * @return
         */
        @AliasFor(annotation = Ann1.class,value = "value")
        String value1() default "ann";
    }

    @Ann2(value = "shiguiwu",value1 = "99999")
    public static class UseAnn1 {

    }

    //互为别名
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Ann3 {

        @AliasFor("v2")
        String v1() default "";


        @AliasFor("v1")
        String v2() default "";

    }

    @Ann3(v1 = "shiguiwu")
    public static class UseAnn3 {

    }

    //当@AliasFor中不指定value或者attribute的时候，自动将@AliasFor修饰的参数作为value和attribute的
    //值，如下@AliasFor注解的value参数值为name

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Ann4 {
        String name() default "";

    }

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Ann4
    @interface Ann41 {

        @AliasFor(annotation = Ann4.class)
        String name() default "";

    }

    @Ann41(name = "我是name!!")
    public static class UseAnn4 {

    }


    public static void main(String[] args) {
        System.out.println(AnnotatedElementUtils.getMergedAnnotation(UseAnn1.class, Ann2.class));
        System.out.println(AnnotatedElementUtils.getMergedAnnotation(UseAnn1.class, Ann1.class));

        System.out.println("===================================================================");


        System.out.println(AnnotatedElementUtils.getMergedAnnotation(UseAnn3.class, Ann3.class));

        System.out.println("===================================================================");

        System.out.println(AnnotatedElementUtils.getMergedAnnotation(UseAnn4.class, Ann41.class));
        System.out.println(AnnotatedElementUtils.getMergedAnnotation(UseAnn4.class, Ann4.class));

    }
}
